package com.asimplefox.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

/**
 * @author github.kloping
 */
@Slf4j
public class TranslateUtils {
    public static final RestTemplate TEMPLATE = new RestTemplate();

    public static String translateToEn(String word) {
        try {
            String url = "https://api.pearktrue.cn/api/translate/?text=" + word + "&type=AUTO";
            JSONObject jo = JSONObject.parseObject(TEMPLATE.getForObject(url, String.class));
            return jo.getJSONObject("data").getString("translate");
        } catch (Exception e) {
            log.error("translate error", e);
            return "translate error";
        }
    }
}
